import 'dart:io';

import 'package:fluro/fluro.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:message_tools/application.dart';
import 'package:message_tools/router/index.dart';
import 'package:message_tools/store/index.dart';
import 'package:message_tools/ui/page/home_page.dart';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';

import 'lang/zh.dart';

void main() async {
  /*String device_id = await DeviceId.getID;
  String imei = await DeviceId.getIMEI;
  String meid = await DeviceId.getMEID;

  debugPrint("device_id = $device_id \n");
  debugPrint("imei = $imei \n");
  debugPrint("meid = $meid \n");*/

  String sDCardDir;
  Future<void> getSDCardDir() async {
    sDCardDir = (await getExternalStorageDirectory()).path;
    Application.setString("sDCardDir", sDCardDir);
    debugPrint("sDCardDir = $sDCardDir \n");
  }

  // Permission check
  Future<void> getPermission() async {
    if (Platform.isAndroid) {
      PermissionStatus permission = await PermissionHandler()
          .checkPermissionStatus(PermissionGroup.storage);
      if (permission == PermissionStatus.denied) {
        await PermissionHandler().requestPermissions([PermissionGroup.storage]);
      }

      permission = await PermissionHandler()
          .checkPermissionStatus(PermissionGroup.phone);
      if (permission == PermissionStatus.denied) {
        await PermissionHandler().requestPermissions([PermissionGroup.phone]);
      }

      permission =
          await PermissionHandler().checkPermissionStatus(PermissionGroup.sms);
      if (permission == PermissionStatus.denied) {
        await PermissionHandler().requestPermissions([PermissionGroup.sms]);
      }

      permission = await PermissionHandler()
          .checkPermissionStatus(PermissionGroup.microphone);
      if (permission == PermissionStatus.denied) {
        await PermissionHandler()
            .requestPermissions([PermissionGroup.microphone]);
      }

      await getSDCardDir();
    } else if (Platform.isIOS) {
      await getSDCardDir();
    }
  }

  Future.wait([getPermission()]).then((result) {
    debugPrint("sDCardDir001 = $sDCardDir \n");
    runApp(MyApp());
  });

  // Android状态栏透明
  SystemChrome.setSystemUIOverlayStyle(
      SystemUiOverlayStyle(statusBarColor: Colors.transparent));
}

class MyApp extends StatelessWidget {
  MyApp() {
    final router = new Router();
    Routes.configureRoutes(router);
    Application.router = router;
  }

  var homePage = HomePage();

  @override
  Widget build(BuildContext context) {
    return Store.init(
        context: context,
        child: MaterialApp(
          title: '短信群发',
          home: Builder(
            builder: (context) {
              return homePage;
            },
          ),
          debugShowCheckedModeBanner: false,
          onGenerateRoute: Application.router.generator,
          localizationsDelegates: <LocalizationsDelegate<dynamic>>[
            ChineseCupertinoLocalizations.delegate, // 这里加上这个,是自定义的delegate

            DefaultCupertinoLocalizations.delegate, // 这个截止目前只包含英文

            // 下面两个是Material widgets的delegate, 包含中文
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
          ],
          supportedLocales: [
            const Locale('en', 'US'), // English
            const Locale('zh', 'Hans'), // China
            const Locale('zh', ''), // China
            // ... other locales the app supports
          ],
        ));
  }
}
